package com.demo.controllers;

import com.demo.database.data.TDemoDepartment;
import com.demo.database.data.TDemoEmployee;
import com.demo.database.data.TDemoPosition;
import com.demo.services.IDepartmentService;
import com.demo.services.IEmployeeService;
import com.demo.services.IPositionService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * 员工管理控制器
 * @cuthor luoxiaojiang
 * @createTime 2019年11月07日16:06:50
 * @Verson 1.0.0
 */
@Controller
public class EmployeeController {

    //注入员工管理业务接口对象
    @Resource
    private IEmployeeService iemployeeService;
    @Resource
    private IPositionService ipositionService;
    @Resource
    private IDepartmentService idepartmentService;

    /**
     * 查询所有员工数据
     * @param model
     * @return
     */
    @RequestMapping("/employeeQurey.html")
    public String employeeQurey(Model model,@RequestParam("pageNum") Integer pageNum){
        try {
            if (pageNum == null || pageNum < 1) {
                pageNum = 1;// 页码，从1开始
            }
            Integer pageSize = 8;// 每页显示数据
            PageHelper.startPage(pageNum, pageSize);// 指定开始分页
            List<TDemoEmployee> list = iemployeeService.employeeQuery("");
            PageInfo<TDemoEmployee> page = new PageInfo<>(list);
            //封装模型数据
            model.addAttribute("page",page);
            //返回下个视图页面
            return "admin/employee/employeeIndex";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    /**
     * 按模糊条件查询员工数据
     * @param searchContext
     * @param model
     * @return
     */
    @RequestMapping("/employeeSearch.html")
    public String employeeSearch(String searchContext, Model model, @RequestParam("pageNum") Integer pageNum){
        try {
            if (pageNum == null || pageNum < 1) {
                pageNum = 1;// 页码，从1开始
            }
            Integer pageSize = 8;// 每页显示数据
            PageHelper.startPage(pageNum, pageSize);// 指定开始分页
            //访问业务实现接口，查询数据
            List<TDemoEmployee> list = iemployeeService.employeeQuery(searchContext);
            PageInfo<TDemoEmployee> page = new PageInfo<>(list);
            //封装模型数据
            model.addAttribute("page",page);
            model.addAttribute("operating","search");
            model.addAttribute("searchContext",searchContext);
            //返回下个视图页面
            return "admin/employee/employeeIndex";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    /**
     * 打开添加员工视图页面
     * @param model
     * @return
     */
    @RequestMapping("/addEmployeeView.html")
    public String addView(Model model,@RequestParam("num") Integer num){
        try{
            //查询部门表和职位表的数据
            List<TDemoPosition> positions = ipositionService.positionQuery();
            List<TDemoDepartment> departments = idepartmentService.departmentQuery();
            model.addAttribute("positionList",positions);
            model.addAttribute("departmentList",departments);
            model.addAttribute("num",num);
            return "admin/employee/employeeAdd";
        } catch (Exception e){
            e.printStackTrace();
        }
        return "error";
    }

    /**
     * 添加员工数据
     * @param employee
     * @param model
     */
    @RequestMapping("/employeeAdd.html")
    public String employeeAdd(TDemoEmployee employee, Model model, @RequestParam("pageNum") Integer pageNum){
        try {
            if (pageNum == null || pageNum < 1) {
                pageNum = 1;// 页码，从1开始
            }
            Integer pageSize = 8;// 每页显示数据
            PageHelper.startPage(pageNum, pageSize);// 指定开始分页
            List<TDemoEmployee> employeeList = iemployeeService.employeeAdd(employee);
            PageInfo<TDemoEmployee> page = new PageInfo<>(employeeList);
            //封装模型数据
            model.addAttribute("page",page);
            return "admin/employee/employeeIndex";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    /**
     * 删除员工数据
     * @param model
     * @param employeeIds
     * @return
     */
    @RequestMapping("/employeeDel.html")
    public String employeeDel(Model model,@RequestParam("pageNum") Integer pageNum,@RequestParam("employeeId") String...employeeIds){
        try {
            if (pageNum == null || pageNum < 1) {
                pageNum = 1;// 页码，从1开始
            }
            Integer pageSize = 8;// 每页显示数据
            PageHelper.startPage(pageNum, pageSize);// 指定开始分页
            List<TDemoEmployee> employeeList = iemployeeService.employeeDel(employeeIds);
            PageInfo<TDemoEmployee> page = new PageInfo<>(employeeList);
            //封装模型数据
            model.addAttribute("page",page);
            return "admin/employee/employeeIndex";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    /**
     * 打开修改页面的视图
     * @param employeeId
     * @param model
     * @return
     */
    @RequestMapping("/updEmployeeView.html")
    public String udpView(Integer employeeId, Model model,@RequestParam("num") Integer num){
        try {
            List<TDemoPosition> positions = ipositionService.positionQuery();
            List<TDemoDepartment> departments = idepartmentService.departmentQuery();
            model.addAttribute("positionList",positions);
            model.addAttribute("departmentList",departments);
            TDemoEmployee employee = iemployeeService.employeeQueryID(employeeId);
            model.addAttribute("employee",employee);
            model.addAttribute("num",num);
            return "admin/employee/employeeUdp";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }

    @RequestMapping("/employeeUpd.html")
    public String employeeUpd(TDemoEmployee employee, Model model, @RequestParam("pageNum") Integer pageNum){
        try {
            if (pageNum == null || pageNum < 1) {
                pageNum = 1;// 页码，从1开始
            }
            Integer pageSize = 8;// 每页显示数据
            PageHelper.startPage(pageNum, pageSize);// 指定开始分页
            List<TDemoEmployee> employeeList = iemployeeService.employeeUpd(employee);
            PageInfo<TDemoEmployee> page = new PageInfo<>(employeeList);
            //封装模型数据
            model.addAttribute("page",page);
            return "admin/employee/employeeIndex";
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "error";
    }
}
